package com.lib.model.regi;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.lib.model.dto.BookDto;
import com.lib.model.dto.MemberDto;
import com.lib.model.dto.ZipCodeDto;

public class MemberDao extends SqlMapClientDaoSupport  implements IMemberDao {

	@Override
	public ArrayList<ZipCodeDto> search(String dong) {
		
		ArrayList<ZipCodeDto> zlist = null;
		
			zlist = (ArrayList<ZipCodeDto>)getSqlMapClientTemplate().queryForList("zipcode.getZipCode",dong);
		
		return zlist;
	}

	@Override
	public boolean addMember(MemberDto dto) {
		int check = 0;
		
		try {
			getSqlMapClientTemplate().insert("LoginMap.addmember",dto);
			check = 1;
		} catch (Exception e) {
			check = -1;
		}
		
		return check>0?true:false;
	}

	@Override
	public int getAllMemberCount() {
		int count = 0;
		try {
        	count = (Integer)
        	getSqlMapClientTemplate().queryForObject("LoginMap.getAllMemberCount");
        } catch(Exception e) {
            System.out.println("isCount : "+e);
        } 
		return count;
	}

	@Override
	public ArrayList<MemberDto> getAllMember(int startn, int endn) {
		ArrayList<MemberDto> booklist = null;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("startpage", startn);
		map.put("endpage", endn);
			booklist = (ArrayList<MemberDto>)getSqlMapClientTemplate().queryForList("LoginMap.getAllMember", map);
		return booklist;
	}

	@Override
	public int getAllMemberCount(String searchType, String searchValue) {
		int count=0;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchType", searchType);
		map.put("searchValue", searchValue);

			count = (Integer)getSqlMapClientTemplate().queryForObject("LoginMap.getAllMemberCount",map);
		
		
		return count;
	}

	@Override
	public ArrayList<MemberDto> getAllMember(int startn, int endn, String searchType, String searchValue) {
		ArrayList<MemberDto> list = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startpage", startn);
		map.put("endpage", endn);
		map.put("searchType", searchType);
		map.put("searchValue", searchValue);

			list = (ArrayList<MemberDto>)getSqlMapClientTemplate().queryForList("LoginMap.getAllMember",map);
		return list;
	}

	@Override
	public MemberDto getMemberDetail(String id) {
		MemberDto dto = null;

			dto = (MemberDto)getSqlMapClientTemplate().queryForObject("LoginMap.getMemberDetail", id);
		return dto;
	}

	@Override
	public ArrayList<MemberDto> getAllMember(String[] ids) {
		ArrayList<MemberDto> list = null;
			MemberDto temp = new MemberDto();
			for(int i=0; i<ids.length; i++){
				temp = (MemberDto)getSqlMapClientTemplate().queryForObject("LoginMap.getAllMember",ids[i]);
				list.add(temp);
			}
		return list;
	}

	@Override
	public boolean setAuth(MemberDto dto) {
		boolean flag = false;
		try {
			this.getSqlMapClientTemplate().update("LoginMap.setAuth", dto);
			flag = true;
		} catch (Exception e) {
		}
		return flag;
	}

}
